package com.my.easyrules.db.mysql.controller;

import com.my.easyrules.db.BizRulesBuild;
import com.my.easyrules.db.mysql.domain.InterfaceRule;
import com.my.easyrules.db.mysql.service.InterfaceRuleService;
import com.my.easyrules.db.service.AuditService;
import com.my.easyrules.db.service.RuleParam;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author timo 2021/3/4
 */
@RestController
@RequestMapping("interfaceRule")
@Slf4j
public class InterfaceRuleController {

    @Autowired
    private InterfaceRuleService interfaceRuleService;
    @Autowired
    private BizRulesBuild bizRulesBuild;

    final String RULE_NAME ="审核规则";

    final String RE_AUDIT_STAGE = "2";

    @Autowired
    private RulesEngine rulesEngine;

    @GetMapping("/list")
    public List<InterfaceRule> findAll(){
        //上海复审
        RuleParam dto = RuleParam
                .builder()
                .businessCode(RE_AUDIT_STAGE)
                .cityCode("110000")
                .build();
        //定义事实
        Facts facts = new Facts();
        facts.put("param", dto);
        facts.put("auditService", AuditService.class);
        Rules rules = bizRulesBuild.fetchBuildRules(RULE_NAME);
        // fire rules on known facts
//        RulesEngine rulesEngine = new DefaultRulesEngine();
//        rulesEngine.fire(rules, facts);
//        SpringContextUtil.getBean(RulesEngine.class).fire(rules, facts);
        rulesEngine.fire(rules, facts);
//        RulesEngine rulesEngine = new DefaultRulesEngine();
//        rulesEngine.fire(rules, facts);
        return null;
//        return interfaceRuleService.list(null);
    }
    @RequestMapping("/save")
    public Boolean save(@RequestBody InterfaceRule interfaceRule){
        interfaceRuleService.save(interfaceRule);
        return Boolean.TRUE;
    }
}
